#define FILEIN freopen("color.in","r",stdin);freopen("color.out","w",stdout);

#include<bits/stdc++.h>
#define int long long
using namespace std;
int t,n,a[10000005],st;map<tuple<int,int,int>,int>dp,visp;
int dfs(int x,int l0,int l1){
    if(x>n)return 0;
    if(clock()-st>CLOCKS_PER_SEC*.9)return 0;
    if(visp[make_tuple(x,l0,l1)])return dp[make_tuple(x,l0,l1)];
    return dp[make_tuple(x,l0,l1)]=max(
        dfs((visp[make_tuple(x,l0,l1)]=1)+x,a[x],l1)+(l0==a[x])*a[x],
        dfs(1+x,l0,a[x])+(l1==a[x])*a[x]
    );
}
signed main(){
    FILEIN;st=clock();
    cin>>t;while(t--){cin>>n;
    for(int i=1;i<=n;i++)cin>>a[i];
    dp.clear();visp.clear();
    cout<<dfs(1,-1,-1)<<endl;}
}
/*
1      color1.ans  detect       detect3.ans  detect5.in   duel2.ans  duel4.in
11     color1.in   detect1.ans  detect3.in   detect5.out  duel2.in   duel4.out
1.cpp  color2.ans  detect1.in   detect3.out  detect.cpp   duel2.out  duel.cpp
aaaa   color2.in   detect1.out  detect4.ans  duel         duel3.ans  sydou6y
a.cpp  color.cpp   detect2.ans  detect4.in   duel1.ans    duel3.in   xyt.in
a.out  color.in    detect2.in   detect4.out  duel1.in     duel3.out  xyt.out
color  color.out   detect2.out  detect5.ans  duel1.out    duel4.ans

~/Desktop/JS-S00394/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11/11$ pwd|tr / ';'|python3 -c"print(input().count(';'))"
116
*/